home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
jpeg
/
jrdsgi.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
152 lines
/*
* jrdsgi.c
*
* Copyright (C) 1991, 1992, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains routines to read input images in SGI format.
*
* These routines use the SGI image library functions found in -limage
*
* Paul Haeberli - 1993
*/
#include "jinclude.h"
#ifdef SGI_SUPPORTED
#include "gl/image.h"
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
LOCAL short *rbuf;
LOCAL short *gbuf;
LOCAL short *bbuf;
LOCAL int cury;
LOCAL IMAGE *image;
/*
* Read the file header; return image size and component count.
*/
LOCAL int isimagemagic(magic)
unsigned short magic;
{
if(magic == IMAGIC)
return 1;
magic = (magic>>8)&0xff + (magic<<8)&0xff00;
if(magic == IMAGIC)
return 1;
else
return 0;
}
/*
* Read one row of pixels.
* This version is used for B/W SGI images:
*/
METHODDEF void
get_BW_pixel_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
/* This version is for reading 8-bit grayscale pixels */
{
register JSAMPROW ptr0;
register long col, width;
getrow(image,rbuf,image->ysize-1-cury,0);
ptr0 = pixel_row[0];
width = cinfo->image_width;
for (col = 0; col<width; col++)
*ptr0++ = (JSAMPLE)rbuf[col];
cury++;
}
/*
* Read one row of pixels.
* This version is used for B/W SGI images:
*/
METHODDEF void
get_RGB_pixel_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
/* This version is for reading 8-bit grayscale pixels */
{
register JSAMPROW ptr0, ptr1, ptr2;
register long col, width;
getrow(image,rbuf,image->ysize-1-cury,0);
getrow(image,gbuf,image->ysize-1-cury,1);
getrow(image,bbuf,image->ysize-1-cury,2);
ptr0 = pixel_row[0];
ptr1 = pixel_row[1];
ptr2 = pixel_row[2];
width = cinfo->image_width;
for (col = 0; col<width; col++) {
*ptr0++ = (JSAMPLE)rbuf[col];
*ptr1++ = (JSAMPLE)gbuf[col];
*ptr2++ = (JSAMPLE)bbuf[col];
}
cury++;
}
METHODDEF void
input_init (compress_info_ptr cinfo)
{
unsigned char hdrbuf[2]; /* workspace for reading control blocks */
/* Read and verify SGI Header */
if (! ReadOK(cinfo->input_file, hdrbuf, 2))
ERREXIT(cinfo->emethods, "Can't read file");
if (! isimagemagic((hdrbuf[0]<<8)+hdrbuf[1]) )
ERREXIT(cinfo->emethods, "Not an SGI file");
lseek(fileno(cinfo->input_file),0L,0);
image = (IMAGE *)fiopen(fileno(cinfo->input_file),"r");
if (! image )
ERREXIT(cinfo->emethods, "Error on SGI image open");
/* Return info about the image. */
if(image->zsize<3) {
cinfo->input_components = 1;
cinfo->in_color_space = CS_GRAYSCALE;
cinfo->methods->get_input_row = get_BW_pixel_row;
} else {
cinfo->input_components = 3;
cinfo->in_color_space = CS_RGB;
cinfo->methods->get_input_row = get_RGB_pixel_row;
}
cinfo->image_width = image->xsize;
cinfo->image_height = image->ysize;
cinfo->data_precision = 8;
rbuf = (short *)malloc(image->xsize*sizeof(short));
gbuf = (short *)malloc(image->xsize*sizeof(short));
bbuf = (short *)malloc(image->xsize*sizeof(short));
cury = 0;
TRACEMS2(cinfo->emethods, 1, "%ux%ux SGI image",
(unsigned int) image->xsize, (unsigned int) image->ysize);
}
/*
* Finish up at the end of the file.
*/
METHODDEF void
input_term (compress_info_ptr cinfo)
{
free(image);
free(rbuf);
free(gbuf);
free(bbuf);
}
/*
* The method selection routine for SGI format input.
* Note that this must be called by the user interface before calling
* jpeg_compress. If multiple input formats are supported, the
* user interface is responsible for discovering the file format and
* calling the appropriate method selection routine.
*/
GLOBAL void
jselrsgi (compress_info_ptr cinfo)
{
cinfo->methods->input_init = input_init;
cinfo->methods->input_term = input_term;
}
#endif /* SGI_SUPPORTED */